<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <!--头像 -->
    <input
      type="file"
      hidden
      ref="file"
      @change="onChange"
    >
    <!-- /导航栏 -->
    <van-cell
      title="头像"
      is-link
      @click="$refs.file.click()"
    >
      <van-image
        class="avatar"
        fit="cover"
        round
        :src="user.photo"
      />
    </van-cell>
    <van-cell
      title="昵称"
      :value="user.name"
      is-link
      @click="isShowUpdateName=true"
    />
    <van-cell
      title="性别"
      :value="user.gender === 0 ? '男' : '女'"
      is-link
      @click="isShowUpdateGender=true"
    />
    <van-cell
      title="生日"
      :value="user.birthday"
      is-link
      @click="isShowUpdateBirthdy=true"
    />

    <!-- 三个弹层+一头像 -->
    <!-- 编辑昵称弹层 -->
    <van-popup
      v-model="isShowUpdateName"
      style="height: 100%"
      position="bottom"
    >
      <updateName
        v-if="isShowUpdateName"
        v-model="user.name"
        @close="isShowUpdateName=false"
      />
    </van-popup>
    <!-- 编辑昵称弹层 -->

    <!-- 编辑性别弹层 -->
    <van-popup
      v-model="isShowUpdateGender"
      position="bottom"
    >
      <updateGender
        v-if="isShowUpdateGender"
        v-model="user.gender"
        @close="isShowUpdateGender = false"
      />
    </van-popup>

    <!-- 编辑生日弹层 -->
    <van-popup
      v-model="isShowUpdateBirthdy"
      position="bottom"
    >
      <UpdateBirthday
        v-if="isShowUpdateBirthdy"
        v-model="user.birthday"
        @close="isShowUpdateBirthdy=false"
      />
    </van-popup>
    <!-- 编辑生日弹层 -->

    <!-- 编辑头像弹层 -->
    <van-popup
      v-model="isShowUpdatePhoto"
      style="height: 100%"
      position="bottom"
    >
      <updatePhoto
        v-if="isShowUpdatePhoto"
        @update-photo="user.photo = $event"
        @close="isShowUpdatePhoto = false"
        :img="img"
      />
    </van-popup>
    <!-- 编头像弹层 -->
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'

import updateName from './components/update-name.vue'
import updateGender from './components/update-gender.vue'
import UpdateBirthday from './components/update-birthday.vue'
import updatePhoto from './components/update-photo.vue'
export default {
  name: 'UserProfile',
  components: {
    updateName,
    updateGender,
    UpdateBirthday,
    updatePhoto
  },
  props: {},
  data () {
    return {
      user: {},
      isShowUpdateName: false,
      isShowUpdateGender: false,
      isShowUpdateBirthdy: false,
      isShowUpdatePhoto: false,
      img: null
    }
  },
  computed: {},
  watch: {},
  created () {
    this.loadUserProfile()
  },
  methods: {
    async loadUserProfile () {
      try {
        const { data } = await getUserProfile()
        console.log(data)
        this.user = data.data
      } catch (err) {
        this.$toast.fail('获取失败，稍后重试')
      }
    },
    // 头像图片
    onChange () {
      // 文件
      const file = this.$refs.file.files[0]
      // bolb
      this.img = window.URL.createObjectURL(file)
      this.isShowUpdatePhoto = true
      this.$refs.file.value = ''
    }
  }
}
</script>

<style scoped lang='less'>
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
}
.van-popup {
  background: #f5f7f9;
}
.left-arrow {
  color: #fff;
}
</style>
